package com.cw.jeeyt.service.impl.system;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.service.bo.system.SystemAboutUsBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.system.SystemAboutUsDao;
import com.cw.jeeyt.dao.model.system.SystemAboutUsDo;
import com.cw.jeeyt.service.converter.system.SystemAboutUsConverter;
import com.cw.jeeyt.service.intf.system.SystemAboutUsService;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * 关于我们业务实现类
 *
 * @author chenw
 * @version 1.0.0
 */
@Service
public class SystemAboutUsServiceImpl implements SystemAboutUsService {

    private static final Logger logger = LoggerFactory.getLogger(SystemAboutUsServiceImpl.class);
    @Autowired
    private SystemAboutUsDao systemAboutUsDao;

    /**
     * 查询所有关于我们
     */
    @Override
    public List<SystemAboutUsBo> querySystemAboutUsList() throws Exception {
        return SystemAboutUsConverter.doList2BoList(systemAboutUsDao.querySystemAboutUsList());
    }

    /**
     * 分页查询关于我们
     *
     * @param systemAboutUsBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<SystemAboutUsBo> queryByPage(SystemAboutUsBo systemAboutUsBo, Integer startNum, Integer pageSize)
    throws Exception {
        PageInfo<SystemAboutUsDo> pageInfo = systemAboutUsDao.queryByPage(SystemAboutUsConverter.bo2Do(systemAboutUsBo),
            startNum, pageSize);
        return new PageInfo<>(pageInfo, SystemAboutUsConverter.doList2BoList(pageInfo.getList()));
    }

    /**
     * 根据主键查寻关于我们信息
     *
     * @param id
     * @return
     */
    @Override
    public SystemAboutUsBo getSystemAboutUsById(Long id) throws Exception {

        if (null == id) {
            logger.error("--主键id为空--");
            return null;
        }

        return SystemAboutUsConverter.do2Bo(systemAboutUsDao.getById(id));
    }

    /**
     * 新增关于我们
     *
     * @param systemAboutUsBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insert(SystemAboutUsBo systemAboutUsBo) throws Exception {

        if (systemAboutUsBo == null) {
            logger.error("关于我们为空");
            return null;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        SystemAboutUsDo systemAboutUs = SystemAboutUsConverter.bo2Do(systemAboutUsBo);
        systemAboutUs.setCreateBy(userVo.getId());
        systemAboutUs.setCreateDate(new Date());
        systemAboutUs.setUpdateBy(userVo.getId());
        systemAboutUs.setUpdateDate(new Date());
        Integer result = systemAboutUsDao.insert(systemAboutUs);
        return result;
    }

    /**
     * 更新关于我们信息
     *
     * @param systemAboutUsBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(SystemAboutUsBo systemAboutUsBo) throws Exception {

        if (systemAboutUsBo == null) {
            logger.error("关于我们为空");
            return;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        SystemAboutUsDo systemAboutUs = SystemAboutUsConverter.bo2Do(systemAboutUsBo);
        systemAboutUs.setUpdateBy(userVo.getId());
        systemAboutUs.setUpdateDate(new Date());

        systemAboutUsDao.update(systemAboutUs);
    }

    /**
     * 删除关于我们
     *
     * @param systemAboutUsBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(SystemAboutUsBo systemAboutUsBo) throws Exception {

        if (systemAboutUsBo == null) {
            logger.error("关于我们为空");
            return;
        }
        systemAboutUsDao.delete(SystemAboutUsConverter.bo2Do(systemAboutUsBo));
    }
}
